﻿using System;
using System.Windows.Forms;
using Seagull.Configuration;
using Seagull.Generic;
using Seagull.Shell.Utils;

namespace Seagull.Shell.Windows
{
    public partial class ConfirmClearRemovableDiskWindow : Form
    {
        private const int FlashWindowTimes = 3;

        private int _countDown = Config.Instance.TaskConfig.CleanUpRemovableDiskCountDownSeconds;

        public ConfirmClearRemovableDiskWindow()
        {
            InitializeComponent();
        }

        public string Drive { get; set; }

        public string DataFolderName { get; set; }

        public event EventHandler<EventArgs<string>> ClearConfirmed;

        private void OnClearConfirmed(string e)
        {
            var handler = ClearConfirmed;
            if(handler != null)
                handler(this, new EventArgs<string>(e));
        }

        private void RefreshPrompt()
        {
            label1.Text = string.Format("移动硬盘{0}不是空的，是否要删除{1}以外的其他目录？",
                Drive, DataFolderName);
        }

        private void OnWindowLoad(object sender, EventArgs e)
        {
            RefreshPrompt();
            RefreshCountDown();
            _countDownTimer.Start();
            WindowFlasher.Flash(this, FlashWindowTimes);

        }

        private void OnCountDownTimerTick(object sender, EventArgs e)
        {
            if(--_countDown == 0)
            {
                _countDownTimer.Stop();
                OnYesClick(_yes, EventArgs.Empty);
            }
            else
            {
                RefreshCountDown();
            }
        }

        private void RefreshCountDown()
        {
            label2.Text = string.Format("将在{0}分{1}秒后自动删除", _countDown / 60, _countDown % 60);
        }

        private void OnYesClick(object sender, EventArgs e)
        {
            _countDownTimer.Stop();
            DialogResult = DialogResult.Yes;
            Close();
            OnClearConfirmed(Drive);
        }

        private void OnNoClick(object sender, EventArgs e)
        {
            _countDownTimer.Stop();
            DialogResult = DialogResult.No;
            Close();
        }
    }
}